﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using System.Net;
using System.Web.Mvc;
using UCenter.Core.Security;

namespace UCenter.Core
{
    public static class Utils
    {
        #region Private members

        private static string _RelativeWebRoot;
        private static string _ThemesRoot;

        #endregion

        #region Public members

        public static string RelativeWebRoot
        {
            get
            {
                if (_RelativeWebRoot == null)
                    _RelativeWebRoot = VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["UCenter.VirtualPath"]);

                return _RelativeWebRoot;
            }
        }

        public static string ThemesRoot
        {
            get
            {
                if(_ThemesRoot == null)
                    _ThemesRoot = VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["UCenter.ThemesPath"]);
                return _ThemesRoot;
            }
        }

        public static Uri AbsoluteWebRoot
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context == null)
                    throw new Exception("当前HttpContext为空");

                if (context.Items["absoluteurl"] == null)
                    context.Items["absoluteurl"] = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority) + RelativeWebRoot);

                return context.Items["absoluteurl"] as Uri;
            }
        }

        public static string DatabaseConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["UCenter.DBConnection"].ConnectionString;
            }
        }

        public static PasswordFormat PasswordFormat
        {
            get
            {
                String formatString = ConfigurationManager.AppSettings["UCenter.PasswordFormat"];
                PasswordFormat format = PasswordFormat.Clear;

                switch (formatString)
                {
                    case "Encrypted":
                        format = PasswordFormat.Encrypted;
                        break;
                    case "Hashed":
                        format = PasswordFormat.Hashed;
                        break;
                    default:
                        break;
                }

                return format;
            }
        }

        #endregion
    }
}
